class UF:  # union find
    def __init__(self, n=10):
        self.n = n
        self.f = [0] * (n + 1)
        for i in range(1, n + 1):
            self.f[i] = i

    def find(self, x):
        if x == self.f[x]:
            return x
        else:
            return self.find(self.f[x])

    def union(self, x, y):
        fx = self.find(x)
        fy = self.find(y)
        self.f[fy] = fx

    def count(self):
        tot = 0
        for i in range(1, n + 1):
            if self.f[i] == i:
                tot += 1

        return tot


n = 5
uf = UF(n)  #
print(uf.find(3))
uf.union(1, 2)
uf.union(2, 3)
uf.union(4, 5)
print(uf.count())
